import { GameRouteData, localApi } from "js/core/localApi"
import bagHandle from "js/handle/bagHandle"
import playerHandle from "js/handle/playerHandle"
import { BagType, ItemType, TownBuildType } from "js/typeDefine/typeDefine"
import { configByID, gameConfig, gameConfigHandle } from "js/config/gameConfig"
import { ItemModel } from "js/model/itemModel"
import itemHandle from "js/handle/itemHandle"
import utils from "js/core/utils"
import equipmentHandle from "js/handle/equipmentHandle"
import gameUtils from "js/core/gameUtils"
import treasureHandle from "js/handle/treasureHandle"
import { TitleInfo } from "js/model/playerModel"

class CheatRoute {
    @localApi("cheatRoute")
    addGold(value: number, gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        gameUtils.setPlayerTestInfo(player, "cheataddGold", value, 1)
        player.itemInfo.diamond += value
        return { err: 0 }
    }
    @localApi("cheatRoute")
    addMoney(value: number, gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        player.itemInfo.gold += value
        return { err: 0 }
    }

    @localApi("cheatRoute")
    addExp(value: number, gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        gameUtils.setPlayerTestInfo(player, "cheatexp", value, 1)
        player.teamList.list.forEach(partner => {
            if (partner) {
                partner.exp += value
            }
        })
        return { err: 0 }
    }
    @localApi("cheatRoute")
    addVipExp(value: number, gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        player.baseInfo.vipExp += value

        return { err: 0 }
    }
    @localApi("cheatRoute")
    addVipTime(value: number, gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        player.baseInfo.vipTime += value * 1000 * 3600 * 24

        return { err: 0 }
    }
    @localApi("cheatRoute")
    resetSignIn(gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        player.baseInfo.signIn = 1
        return { err: 0 }
    }
    @localApi("cheatRoute")
    resetTask(gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        player.task.doneAmount = 0
        player.task.list = {}
        return { err: 0 }
    }
    //添加宝石
    @localApi("cheatRoute")
    addGem(gemID: number, gemLvl: number, amount: number, gamRouteData?: GameRouteData) {
        gemLvl = gemLvl || 1
        gemID = gemID || 175001
        let { player, time, dayTime, random, } = gamRouteData
        let gem = itemHandle.create({ tID: gemID, stack: amount, hp: gemLvl }, random)
        bagHandle.AddItemComponent(player.bag, gem)
        return { err: 0 }
    }
    //添加物品
    @localApi("cheatRoute")
    addItem(itemID: number, amount: number, gamRouteData?: GameRouteData) {
        itemID = itemID || 170001
        let { player, time, dayTime, random, } = gamRouteData
        bagHandle.AddItem(player.bag, itemID, amount, random)
        return { err: 0 }
    }

    @localApi("cheatRoute")
    addLevel(value: number, gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        player.teamList.list.filter(p => p != null).forEach(partner => {
            partner.lvl += value
            let lvlMax = gameConfigHandle.gameConfig.base.levelMax
            if (partner.lvl > lvlMax)
                partner.lvl = lvlMax
        })
        playerHandle.initTeam(player)
        return { err: 0 }
    }

    @localApi("cheatRoute")
    levelReset(gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        player.teamList.list.forEach(partner => {
            partner.lvl = 1
        })
        playerHandle.initTeam(player)
        return { err: 0 }
    }
    @localApi("cheatRoute")
    clearBag(bagType: BagType, gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        if (bagType == BagType.Bag) {
            bagHandle.clear(player.bag)
        } else if (bagType == BagType.Equipment) {
            bagHandle.clear(player.equipmentBag)
        } else if (bagType == BagType.Treasure) {
            bagHandle.clear(player.treasureBag)
        }
        return { err: 0 }
    }

    @localApi("cheatRoute")
    clearPartnerEquipment(gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        player.teamList.list.forEach(partner => {
            partner.equipments = []
        })
        return { err: 0 }
    }

    //解锁所有地图
    @localApi("cheatRoute")
    setMapMax(gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        let mapList = gameConfigHandle.gameConfig.base.mapList
        let lastMapID = mapList[mapList.length - 1]
        gameUtils.setMapMax(player, 1209, time)
        player.adventureInfo.floor = 999
        player.adventureInfo.totalFloor = 1209
        player.adventureInfo.mapID = 100022
        return { err: 0 }
    }
    //冒险进度+1
    @localApi("cheatRoute")
    adventureMapFloorNext(gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        if (player.baseInfo.disableAutoMap) {
            return { err: 100 }
        }
        let adventure = player.adventureInfo
        let mapConfig = gameConfigHandle.configByID[adventure.mapID]
        if (!adventure.mapProgress[adventure.mapID]) {
            adventure.mapProgress[adventure.mapID] = 1
        }
        if (adventure.mapProgress[adventure.mapID] < adventure.floor + 1) {
            adventure.mapProgress[adventure.mapID] = adventure.floor + 1
        }
        // let progress = adventure.mapProgress[adventure.mapID] || 1

        // if (progress <= adventure.floor) {
        //     return { err: 100 }
        // }
        if (adventure.floor >= (mapConfig.maxFloor || 3000)) {
            adventure.mapID = mapConfig.nextMapID
            adventure.floor = 1
        } else {
            adventure.floor += 1
        }
        adventure.totalFloor += 1

        return { err: 0 }
    }
    //选择地图冒险
    @localApi("cheatRoute")
    toThisMap(id: number, gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        player.adventureInfo.mapID = id
        player.adventureInfo.floor = 1
        return { err: 0 }
    }

    //获得随机门派宝物
    @localApi("cheatRoute")
    getOneTreasure(gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        let treasureAllIDs = gameConfigHandle.gameConfig.base.treasureList
        let randID = utils.getOneFromArray(treasureAllIDs, random)
        let lvl = gameConfigHandle.configByID[player.adventureInfo.mapID].mapLvl
        let treasureRand = equipmentHandle.createTreasure(randID, lvl, random)
        let res = bagHandle.AddItemComponent(player.treasureBag, treasureRand)
        if (!res) {
            return { err: 1 }
        }
        return { err: 0 }
    }

    //重置门派宝物
    @localApi("cheatRoute")
    treasureRestart(gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        bagHandle.clear(player.treasureBag)
        //更新角色属性 并刷新显示
        playerHandle.initTeam(player)
        return { err: 0 }
    }

    //重置金币
    @localApi("cheatRoute")
    moneyRestart(gameRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gameRouteData
        player.itemInfo.gold = 0
        return { err: 0 }
    }
    //重置魂晶
    @localApi("cheatRoute")
    goldRestart(gameRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gameRouteData
        player.itemInfo.diamond = 0
        return { err: 0 }
    }
    //经验归零
    @localApi("cheatRoute")
    expRestart(gameRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gameRouteData
        player.teamList.list.forEach(partner => {
            partner.exp = 0
        })
        return { err: 0 }
    }
    //重置部位强化
    @localApi("cheatRoute")
    partStrRestart(gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        player.teamList.list.forEach(partner => {
            partner.partStr = {}
        })
        //更新角色属性 并刷新显示
        playerHandle.initTeam(player)
        return { err: 0 }
    }
    //部位强化提升
    @localApi("cheatRoute")
    partStrUp(num: number, gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        player.teamList.list.forEach(partner => {
            let pLvl = partner.lvl
            let parts = gameConfigHandle.gameConfig.equipment.equipmentParts
            parts.forEach(part => {
                let lvl = partner.partStr[part] || 0
                partner.partStr[part] = lvl + num
                if (partner.partStr[part] > pLvl) {
                    partner.partStr[part] = pLvl
                }
            })
        })
        //更新角色属性 并刷新显示
        playerHandle.initTeam(player)
        return { err: 0 }
    }
    //重置属性强化
    @localApi("cheatRoute")
    attriStrRestart(gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        player.teamList.list.forEach(partner => {
            partner.attriStrLvls = {}
        })
        //更新角色属性 并刷新显示
        playerHandle.initTeam(player)
        return { err: 0 }
    }
    //重置爵位
    @localApi("cheatRoute")
    vipRestart(gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        player.baseInfo.vipExp = 0
        //更新角色属性 并刷新显示
        playerHandle.initTeam(player)
        return { err: 0 }
    }
    //晋爵
    @localApi("cheatRoute")
    upVip(value, gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random } = gamRouteData
        player.baseInfo.vipExp += value
        //更新角色属性 并刷新显示
        playerHandle.initTeam(player)
        return { err: 0 }
    }

    //获得进阶经验
    @localApi("cheatRoute")
    addSkillExp(amount: number, gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        player.baseInfo.skillExp += amount
        return { err: 0 }
    }


    @localApi("cheatRoute")
    resetMapProgress(gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        player.adventureInfo.mapProgress = {}
        player.baseInfo.mapProgressMax = 0
        player.adventureInfo.floor = 1
        player.adventureInfo.totalFloor = 0
        player.adventureInfo.mapID = gameConfigHandle.gameConfig.base.initMapID
        return { err: 0 }
    }
    //重置进阶经验
    @localApi("cheatRoute")
    resetWuxiuExp(gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        player.baseInfo.skillExp = 0
        return { err: 0 }
    }

    @localApi("cheatRoute")
    addEquipment(tID: number, quality: number, lvl: number, gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        let item = equipmentHandle.create(player, tID, lvl, random, quality)
        bagHandle.AddItemComponent(player.equipmentBag, item)
        return { err: 0 }
    }
    @localApi("cheatRoute")
    addTreasure(tID: number, quality: number, lvl: number, gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        let treasure = treasureHandle.createTreasure(tID, lvl, random, quality, 9)
        bagHandle.AddItemComponent(player.treasureBag, treasure)
        return { err: 0 }
    }

    @localApi("cheatRoute")
    getOfflineTime(gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        player.getResourceDate -= 60 * gameConfigHandle.gameConfig.base.getResourceInterval
        return { err: 0 }
    }

    @localApi("cheatRoute")
    resetDay(gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        playerHandle.resetDay(player, random)
        return { err: 0 }
    }
    @localApi("cheatRoute")
    resetMap(gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        player.adventureInfo.mapProgress = {}
        player.baseInfo.mapProgressMax = 0
        player.adventureInfo.mapID = gameConfigHandle.gameConfig.base.initMapID
        player.adventureInfo.floor = 1
        player.adventureInfo.totalFloor = 1
        return { err: 0 }
    }

    //当前装备升星至满星
    @localApi("cheatRoute")
    equipmentOnStrengthFull(gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        player.teamList.list.forEach(partner => {
            let equipmentsOn = partner.equipments[partner.equipmentActive]
            Object.entries(equipmentsOn).forEach(item => {
                let equipment: ItemModel = item[1]
                if (equipment) {
                    let strengthMax = gameConfigHandle.gameConfig.equipment.strMax
                    equipment.equipment.strengthen = strengthMax
                }
            })
        })
        //更新角色属性 并刷新显示
        playerHandle.initTeam(player)
        return { err: 0 }
    }
    //深渊进度+50
    @localApi("cheatRoute")
    abyssProgressUp(gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        return { err: 0 }
    }
    //集市购买进度+50次
    @localApi("cheatRoute")
    marketBuyTimesUp(gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        player.market.buyTimes += 50
        return { err: 0 }
    }
    //巅峰战力+5000
    @localApi("cheatRoute")
    maxPowerUp(gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        player.baseInfo.maxPower += 5000
        return { err: 0 }
    }

    @localApi("cheatRoute")
    addStarLvl(lvl, gamRouteData?: GameRouteData) {
        let { player, time, dayTime, random, } = gamRouteData
        player.teamList.list.forEach(p => {
            p.star += lvl
            if (p.star > gameConfig.partner.maxStar) {
                p.star = gameConfig.partner.maxStar
            }
        })
        return {
            err: 0
        }
    }

    @localApi("cheatRoute")
    setAnimation(key, gamRouteData?: GameRouteData) {
        let { player } = gamRouteData
        player.baseInfo.animationSet[key] = !player.baseInfo.animationSet[key]
        return {
            err: 0
        }
    }

    @localApi("cheatRoute")
    doTask(gamRouteData?: GameRouteData) {
        let { player, time } = gamRouteData
        player.task.taskList.forEach(task => {
            if (task && task.completeTime && task.completeTime > time) {
                task.completeTime = time
            }
        })
        return {
            err: 0
        }
    }


    @localApi("cheatRoute")
    doBarBuild(gamRouteData?: GameRouteData) {
        let { player, time } = gamRouteData
        let entries = Object.entries(player.alchemyInfo.build)
        for (let i = 0; i < entries.length; i++) {
            let entry = entries[i];
            let tid = entry[0]
            let completeTime: any = entry[1]
            if (completeTime > time) {
                player.alchemyInfo.build[tid] = time
            }
        }
        return {
            err: 0
        }
    }

    @localApi("cheatRoute")
    resetNight(gamRouteData?: GameRouteData) {
        let { player, time } = gamRouteData
        player.deepInfo.nightProgress = 0
        player.deepInfo.nightTodayProgress = 0
        player.deepInfo.nightTimes = 0
        return {
            err: 0
        }
    }

    @localApi("cheatRoute")
    descrDiamond(num: number, gamRouteData?: GameRouteData) {
        let { player, time } = gamRouteData
        player.itemInfo.diamond = (player.itemInfo.diamond - num < 0) ? 0 : player.itemInfo.diamond - num
        return {
            err: 0
        }
    }

    @localApi("cheatRoute")
    autoCompleteTownBuild(gamRouteData?: GameRouteData) {
        let { player, time } = gamRouteData
        let newBuildStatus = {}
        Object.entries(player.townInfo.buildStatus).forEach(b => {
            let type: any = b[0]
            if (type == TownBuildType.Town) {
                player.townInfo.lvl += 1
                let boomingValues = gameConfig.town.list[0].boomingValues
                player.townInfo.booming = (player.townInfo.booming || 0) + utils.getIncValue(boomingValues, player.townInfo.lvl)
            } else {
                player.townInfo.subBuildLvl[type] = (player.townInfo.subBuildLvl[type] || 0) + 1
                let boomingValues = gameConfig.town.list.find(l => l.kind == type).boomingValues
                player.townInfo.booming = (player.townInfo.booming || 0) + utils.getIncValue(boomingValues, player.townInfo.subBuildLvl[type])
            }
        })
        player.townInfo.buildStatus = newBuildStatus
        return {
            err: 0
        }
    }

    @localApi("cheatRou te")
    autoCompleteTownTask(gamRouteData?: GameRouteData) {
        let { player, time, random } = gamRouteData
        player.townInfo.taskList.forEach(t => {
            if (t.status == 1) {
                t.status = 2
                t.completeTime = time
                player.townInfo.saveScore += (t.result ? t.saveScore : t.saveScore * -1)
                if (player.townInfo.saveScore < 0) {
                    player.townInfo.saveScore = 0
                } else if (player.townInfo.saveScore > 120) {
                    player.townInfo.saveScore = 120
                }
                let reward = t.reward
                if (reward) {
                    if (reward.itemID == 'treasure') {
                        let item = reward.value
                        if (item) {
                            bagHandle.AddItemComponent(player.treasureBag, item)
                        }
                    } else if (reward.itemID == 'gold') {
                        player.itemInfo.gold += (reward.value || 0)
                    } else if (reward.itemID == 'exp') {
                        player.teamList.list.forEach(p => {
                            if (p) {
                                p.exp += (reward.value || 0)
                            }
                        })
                    } else {
                        bagHandle.AddItem(player.bag, reward.itemID, reward.value, random)
                    }
                }
            }
        })
        return {
            err: 0
        }
    }

    @localApi("cheatRoute")
    refreshTownTask(gamRouteData?: GameRouteData) {
        let { player, time, random } = gamRouteData
        let times = new Date(time).getMinutes() * 10000
        playerHandle.resetTownTask(player, random)
        return {
            err: 0
        }
    }

    @localApi("cheatRoute")
    resetTownSearch(gamRouteData?: GameRouteData) {
        let { player, time, random } = gamRouteData
        player.townInfo.searchTaskList = []
        return {
            err: 0
        }
    }

    @localApi("cheatRoute")
    addTownScore(num, gamRouteData?: GameRouteData) {
        let { player, time, random } = gamRouteData
        player.townInfo.saveScore += num
        if (player.townInfo.saveScore > 120) {
            player.townInfo.saveScore = 120
        }
        if (player.townInfo.saveScore < 0) {
            player.townInfo.saveScore = 0
        }
        return {
            err: 0
        }
    }

    @localApi("cheatRoute")
    addLongVip(gamRouteData?: GameRouteData) {
        let { player, time, random } = gamRouteData
        player.baseInfo.longVip = true
        return {
            err: 0
        }
    }

    @localApi("cheatRoute")
    resetTitles (gamRouteData?: GameRouteData) {
        let { player, time, random } = gamRouteData
        player.titleInfo = new TitleInfo({})
        return {
            err: 0
        }
    }
}

export let cheatRoute: CheatRoute = new CheatRoute()
// export let cheatRouteBak: CheatRoute = new CheatRoute()